var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var oldData = '';
var isEdit = false;
Page({
  data: {
      minHeight: 0,
      sourceName:'',
      arttypeValue:'',
      caizhiValue:'',
      startDate:"请选择",
      endDate:"请选择",
      message:'',
      placeholder:"",
      token: '',
      loginId:0,
      status:""
  },
  loadMore:function(){
      if(!noMore){
        page ++;
        this.load();
      }
  },
  onLoad: function(data){
    var that = this;
    app.LoginUserInfo(function(res){
      if (res && res != -1) {
      that.setData({
        loginId:res.uid,
        token:res.token
      });
      }
    });
    if(data.status == "gzjl"){
      this.setData({
        status:"gzjl",
        placeholder:"工作职责、项目经历和业绩"
      });
      wx.setNavigationBarTitle({
          title: "工作经历"
      });  
    }else if(data.status == "jyjl"){
      this.setData({
        status:"jyjl",
        placeholder:"内容"
      });
      wx.setNavigationBarTitle({
          title: "教育经历"
      });
    }

    oldData = '';
    isEdit = false;
    if (data.data) {//有数据传过来，说明是编辑
      wx.showLoading({
        title: '加载中',
      })
      oldData = JSON.parse(data.data);
      isEdit = true;
    }

    this.load();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (isEdit) {
      var source = oldData.source.username ? oldData.source.username : oldData.source;
      this.setData({
        sourceName: source,
        arttypeValue: oldData.arttype,
        caizhiValue: oldData.caizhi,
        startDate: oldData.starttime,
        endDate: oldData.endtime,
        message: oldData.message,
      })
      wx.hideLoading();
    }
  },

  load:function(){
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          minHeight:res.windowHeight-45
        });
      }
    })
  },
  bindDateChange: function(e){
    var name = e.currentTarget.dataset.name;
    if(name == "start"){
      this.setData({
        startDate: e.detail.value
      })
    }else if(name == "end"){
      this.setData({
        endDate: e.detail.value
      })
    }
  },
  save: function (e) {
    // wx.showLoading({ title: '发布中，请稍等' });
    wx.showLoading({ title: '发布中...' });
    var formData = e.detail.value;
    
    var action = "posttopic";
    if (isEdit) {
      action = "edittopic";
    }
    var data={
        ac: action,
        postuid:this.data.loginId,
        publickey: util.publiccode,
        uid: this.data.loginId,
        privatekey: utilMd5.hexMD5(this.data.loginId + action +this.data.token)
    };
    if (isEdit) {
      data.topicid = oldData.id;
    }

    if(this.data.status == "gzjl"){
      if(!formData.gongsi){
        app.dialog('请输入公司', 'loading');
        return false;
      }
      if(!formData.zhiwei){
        app.dialog('请输入职位', 'loading');
        return false;
      }
      data.source = formData.gongsi;
      data.arttype = formData.zhiwei;
      data.topictype = 19;
    }else{
      if(!formData.xuexiao){
        app.dialog('请输入学校', 'loading');
        return false;
      }
      if(!formData.zhuanye){
        app.dialog('请输入专业', 'loading');
        return false;
      }
      if(!formData.xueli){
        app.dialog('请输入学历', 'loading');
        return false;
      }
      data.source = formData.xuexiao;
      data.arttype = formData.zhuanye;
      data.caizhi = formData.xueli;
      data.topictype = 20;
    }
    if(this.data.startDate == "请选择"){
      app.dialog('请选择开始时间', 'loading');
      return false;
    }
    if(this.data.endDate == "请选择"){
      app.dialog('请选择结束时间', 'loading');
      return false;
    }
    if(!formData.miaoshu){
      app.dialog('请输入经历描述', 'loading');
      return false;
    }
    data.starttime = this.data.startDate;
    data.endtime = this.data.endDate;
    data.message = formData.miaoshu;
    util.api({
      method:"POST",
      data:data,
      success:function(responseData){
        //console.log(responseData);
        //判断是否创建成功 成功了跳转到剪接页面 下面是跳转
        wx.redirectTo({ url: "/pages/index/index" });
      }
    });
  }
})